/*
 *  $Id$

    Copyright (C) 2008  Nathaniel Waisbrot

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package termint.brains;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import termint.game.dungeon.Feature;
import termint.game.dungeon.Room;
import termint.game.dungeon.feature.Fountain;
import termint.game.dungeon.feature.Sink;
import termint.game.dungeon.room.Shop;
import termint.game.dungeon.room.Vault;
import termint.game.dungeon.room.Zoo;

/**
 * A client of MessageWatcher that detects and decodes dungeon noises for listeners down the line
 * @author Nathaniel Waisbrot
 */
public class SoundDecoder implements MessageListener {
	public static interface Listener {
		public void heardRoom(Class<? extends Room> roomType);
		public void heardFeature(Class<? extends Feature> roomType);
	}
	private ArrayList<Listener> listeners = new ArrayList<Listener>();

	private final Map<Pattern,Class<? extends Feature>> featureSounds = new HashMap<Pattern,Class<? extends Feature>>();
	{
		featureSounds.put(Pattern.compile("You hear (?:water falling on coins|the splashing of a naiad|bubbling water|a soda fountain)"), Fountain.class);
		featureSounds.put(Pattern.compile("You hear (?:a slow drip|a gurgling noise|dishes being washed)"), Sink.class);
	}
	private final Map<Pattern, Class<? extends Room>> roomSounds = new HashMap<Pattern,Class<? extends Room>>();
	{
		roomSounds.put(Pattern.compile("You hear (?:a sound reminiscent of a seal barking|a sound reminiscent of an elephant stepping on a peanut|Doctor Doolittle)"), Zoo.class);
		roomSounds.put(Pattern.compile("You hear (?:the footsteps of a guard on patrol|someone counting money|Ebenezer Scrooge|the quarterback calling the play)"), Vault.class);
		roomSounds.put(Pattern.compile("You hear (?:someone cursing shoplifters|the chime of a cash register|Neiman and Marcus arguing)"), Shop.class);
	}
	
	/* (non-Javadoc)
	 * @see termint.brains.MessageListener#newText(java.lang.String)
	 */
	@Override
	public void newText(String text) {
		for (Entry<Pattern, Class<? extends Feature>> entry : featureSounds.entrySet()) {
			if (entry.getKey().matcher(text).find())
				seeFeature(entry.getValue());
		}
		for (Entry<Pattern, Class<? extends Room>> entry : roomSounds.entrySet()) {
			if (entry.getKey().matcher(text).find())
				seeRoom(entry.getValue());
		}
	}

	@Override
	public void clear() {}

	public void addListener(Listener listener) {
		listeners.add(listener);
	}
	
	private void seeFeature(Class<? extends Feature> class1) {
		for (Listener listener : listeners)
			listener.heardFeature(class1);
	}
	private void seeRoom(Class<? extends Room> class1) {
		for (Listener listener : listeners)
			listener.heardRoom(class1);
	}

	@Override
	public void newQuestion(String text) {}
}
